﻿//////////////////////////////////////////////////////////////////////////////
//This file is part of My Nes                                               //
//A Nintendo Entertainment System Emulator.                                 //
//                                                                          //
//Copyright © 2009 Ala Hadid (AHD)                                          //
//                                                                          //
//My Nes is free software; you can redistribute it and/or modify            //
//it under the terms of the GNU General Public License as published by      //
//the Free Software Foundation; either version 2 of the License, or         //
//(at your option) any later version.                                       //
//                                                                          //
//My Nes is distributed in the hope that it will be useful,                 //
//but WITHOUT ANY WARRANTY; without even the implied warranty of            //
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             //
//GNU General Public License for more details.                              //
//                                                                          //
//You should have received a copy of the GNU General Public License         //
//along with this program; if not, write to the Free Software               //
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA//
//////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SilverNes.Emulator.Extensions;

namespace AHD.MyNes.Nes
{
    public static class Video
    {
        // Control Surface = new Control();
		public const int Width = 256;
		public const int Height = 240;
        // public int Alpha = 150;//Works only with 32 bit mode
        public const ushort RedMask = 0xF800;
        public const ushort GreenMask = 0x7E0;
        public const ushort BlueMask = 0x1F;
		public static int[] RenderFrame(short[] screen)
		{
			return screen.Map<short, int>(ConvertToBgr32);
		}
		public static void RenderFrame(short[] screen, int[] target)
		{
			screen.Map(target, ConvertToBgr32);
		}
		public static int ConvertToBgr32(short nesColor)
		{
			byte[] components = new byte[4];
			components[3] = 0;
			components[2] = (byte)(((nesColor & RedMask) >> 11) << 3);
			components[1] = (byte)(((nesColor & GreenMask) >> 5) << 2);
			components[0] = (byte)((nesColor & BlueMask) << 3);

			return BitConverter.ToInt32(components, 0);
		}
		//public void RenderFrame(short[] ScreenBuffer)
		//{
		//    if (!IsTakingScreenShot)
		//    {
		//        IsRedering = true;
		//        // BitmapData bmpData32 = bmp.LockBits(new Rectangle(0, 0, 256, 240), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
		//        // int* numPtr32 = (int*)bmpData32.Scan0;
		//        for (int i = 0; i < ScreenBuffer.Length; i++)
		//        {
		//            //Extract the colors of each pixel to applay masks
		//            //And convert to 32 bit color
                    
		//            numPtr32[i] = Color.FromArgb(Alpha, RedValue, GreenValue, BlueValue).ToArgb();
		//        }
		//        // bmp.UnlockBits(bmpData32);
		//        int Screen_X = 0;
		//        int Screen_Y = 0;
		//        switch (_Size)
		//        {
		//            case DrawSize.Stretch:
		//                GR.DrawImage(bmp, 0, 0, Surface.Size.Width, Surface.Size.Height); break;
		//            case DrawSize.X1:
		//                Screen_X = (Surface.Size.Width / 2) - 128;
		//                Screen_Y = (Surface.Size.Height / 2) - 120;
		//                GR.DrawImage(bmp, Screen_X, Screen_Y, 256, 240); break;
		//            case DrawSize.X2:
		//                Screen_X = (Surface.Size.Width / 2) - 256;
		//                Screen_Y = (Surface.Size.Height / 2) - 240;
		//                GR.DrawImage(bmp, Screen_X, Screen_Y, 512, 480); break;
		//        }
		//        GR.Dispose();
		//        IsRedering = false;
		//    }
		//}
		///// <summary>
		///// Save current screenshot
		///// </summary>
		///// <param name="ScreenshotPath">The full path of screenshot to save into</param>
		///// <param name="Format">The extension of the format (e.g. : .bmp means Bmp format)</param>
		//public void TakeScreenShot(string ScreenshotPath, string Format)
		//{
		//    IsTakingScreenShot = true;
		//    //Whait for the current frame
		//    while (IsRedering)
		//    { }
		//    switch (Format)
		//    {
		//        case ".bmp":
		//            bmp.Save(ScreenshotPath, ImageFormat.Bmp);
		//            IsTakingScreenShot = false;
		//            break;
		//        case ".gif":
		//            bmp.Save(ScreenshotPath, ImageFormat.Gif);
		//            IsTakingScreenShot = false;
		//            break;
		//        case ".jpg":
		//            bmp.Save(ScreenshotPath, ImageFormat.Jpeg);
		//            IsTakingScreenShot = false;
		//            break;
		//        case ".png":
		//            bmp.Save(ScreenshotPath, ImageFormat.Png);
		//            IsTakingScreenShot = false;
		//            break;
		//        case ".tiff":
		//            bmp.Save(ScreenshotPath, ImageFormat.Tiff);
		//            IsTakingScreenShot = false;
		//            break;
		//    }
		//}
    }
}
